package com.wip.reportingservice.daos.impls;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.wip.reportingservice.daos.entities.ClientsTable;
import com.wip.reportingservice.daos.entities.PagesTable;
import com.wip.reportingservice.daos.interfaces.IPagesDao;

@Repository
@Transactional
public class PagesDaoImpl implements IPagesDao {

	private static final Logger logger = LoggerFactory
			.getLogger(PagesDaoImpl.class);

	@Autowired
	private SessionFactory sessionFactory;

	public void save(PagesTable page) {
		logger.info("PagesDaoImpl.save");

		sessionFactory.getCurrentSession().save(page);

	}

	public void update(PagesTable page) {
		logger.info("PagesDaoImpl.update");

		sessionFactory.getCurrentSession().update(page);

	}

	public void delete(PagesTable page) {
		logger.info("PagesDaoImpl.delete");
		sessionFactory.getCurrentSession().delete(page);

	}

	public PagesTable getById(int id) {
		logger.info("PagesDaoImpl.getById");

		final Criteria crit = sessionFactory.getCurrentSession()
				.createCriteria(PagesTable.class);
		crit.add(Restrictions.idEq(id));
		return (PagesTable) crit.list().get(0);

	}

	@Override
	public PagesTable getByName(String name) {
		logger.info("PagesDaoImpl.getById");

		final Criteria crit = sessionFactory.getCurrentSession()
				.createCriteria(PagesTable.class);

		crit.add(Restrictions.ilike("pageName", name));

		return (PagesTable) crit.list().get(0);
	}
	
	@Override
	public  List<PagesTable> getAll() {
		final Criteria crit = sessionFactory.getCurrentSession().createCriteria(PagesTable.class)
			    .setProjection(Projections.projectionList()
			      .add(Projections.property("pageName"), "pageName"))
			    .setResultTransformer(Transformers.aliasToBean(PagesTable.class));

			  List<PagesTable> list = crit.list();
			  
			  return list;
	}

}
